package hpu.e4_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hpu.e4_demo.common.Result;
import hpu.e4_demo.entity.TRole;
import hpu.e4_demo.mapper.TRoleMapper;
import hpu.e4_demo.service.ITRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色类型表 服务实现类
 * </p>
 *
 * @author FFF
 * @since 2025-06-21
 */
@Service
public class TRoleServiceImpl extends ServiceImpl<TRoleMapper, TRole> implements ITRoleService {
    @Autowired
    private TRoleMapper tRoleMapper;

    @Override
    public Result<List<TRole>> getRole(Integer currentPage, Integer size) {
        IPage<TRole> page = new Page<>(currentPage,size);
        IPage<TRole> result = tRoleMapper.findAll(page);
        List<TRole> result2 = result.getRecords();
        return Result.ok(result2);
    }

    @Override
    public Result<String> addRole(TRole tRole) {

        return tRoleMapper.insert(tRole) > 0 ? Result.ok("添加成功") : Result.fail("添加失败");
    }

    @Override
    public Result<String> modifyRole(Integer id,String role) {
        return tRoleMapper.UpdateById(id,role) > 0 ? Result.ok("修改成功") : Result.fail("修改失败");
    }

    @Override
    public Result<String> deleteRole(Integer id) {
        QueryWrapper<TRole> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return tRoleMapper.delete(wrapper) > 0 ? Result.ok("删除成功") : Result.fail("删除失败");
    }
}
